import json

from apps.common.base.serializers import *
from apps.merchant.item.models import *
from apps.merchant.sku.serializers import Sku, SkuModelSerializer

class ItemBaseSerializer(BaseSerializer):
    class Meta:
        model = Item
        fields = "__all__"
        
class ItemForCListSerializer(BaseSerializer):

    priceModel = serializers.SerializerMethodField()
    def get_priceModel(self, obj):
        skus = obj.item_sku.all().order_by('sellingPrice')
        if skus.exists():
            sku = skus.first()
            return {'sellingPrice': str(sku.sellingPrice), 'marketPrice': str(sku.marketPrice), 'discountPrice': str(sku.discountPrice)}
        else:
            return {}
        
    skuList = serializers.SerializerMethodField()
    def get_skuList(self, obj):
        user = None
        if self.context.get('request'):
            user = self.context["request"].user
        skus = obj.item_sku.all().order_by('-sort')
        if skus.exists():
            return SkuModelSerializer(skus, many=True, context={'request': self.context["request"]}).data
        else:
            return []

    # def get_priceModel(self, obj):
    #     return {'sellingPrice': str(obj.sellingPrice), 'marketPrice': str(obj.marketPrice), 'discountPrice': str(obj.discountPrice)}

        
    coverUrl = serializers.SerializerMethodField()
    def get_coverUrl(self, obj):
        if obj.coverPhotos:
            coverPhotos = json.loads(obj.coverPhotos)
            if len(coverPhotos) > 0:
                return coverPhotos[0]

        return ''
        
    class Meta:
        model = Item
        fields = "__all__"


class ItemModelSerializer(BaseSerializer):

    shopModel = serializers.SerializerMethodField()
    def get_shopModel(self, obj):
        try:
            return {'id': obj.shop.id, 'name': obj.shop.name, 'types': obj.shop.types}
        except:
            return {}

    activityModel = serializers.SerializerMethodField()
    def get_activityModel(self, obj):
        try:
            return {'id': obj.activity.id, 'name': obj.activity.name, 'key': obj.activity.key}
        except:
            return {}

    activityKey = serializers.SerializerMethodField()

    def get_activityKey(self, obj):
        try:
            return obj.activity.key
        except:
            return ''

    generaModel = serializers.SerializerMethodField()
    def get_generaModel(self, obj):
        if obj.genera:
            return {'id': obj.genera.id, 'name': obj.genera.name, 'key': obj.genera.key}
        else:
            return {}
        
    cateModel = serializers.SerializerMethodField()
    def get_cateModel(self, obj):
        if obj.cate:
            return {'id': obj.cate.id, 'name': obj.cate.name, 'key': obj.cate.key}
        else:
            return {}
    
    tagList = serializers.SerializerMethodField()
    def get_tagList(self, obj):
        try:
            return [{'id': obj.id, 'name': obj.name, 'key': obj.key} for obj in obj.tags.all()]
        except:
            return []

    priceModel = serializers.SerializerMethodField()
    def get_priceModel(self, obj):
        skus = obj.item_sku.all().order_by('sellingPrice')
        if skus.exists():
            user = None
            if self.context.get('request'):
                user = self.context["request"].user
            sku = skus.first()
            orderItemList = [{'item': sku.item.id, 'sku': sku.id, 'payCount': 1}]
            from apps.pay.order.sceneHandle import setPayMoney
            res = setPayMoney({'orderItemList': orderItemList}, {'scene': 'generay'}, user)
            return res.get('data')
        else:
            return {}

    coverPhotoList = serializers.SerializerMethodField()

    def get_coverPhotoList(self, obj):
        try:
            coverPhotoList = json.loads(obj.coverPhotos)
            if len(coverPhotoList) > 0:
                return coverPhotoList
        except:
            return []

    skuList = serializers.SerializerMethodField()
    def get_skuList(self, obj):
        user = None
        if self.context.get('request'):
            user = self.context["request"].user
        skus = obj.item_sku.all().order_by('-sort')
        if skus.exists():
            return SkuModelSerializer(skus, many=True, context={'request': self.context["request"]}).data
        else:
            return []
        
    coverUrl = serializers.SerializerMethodField()
    def get_coverUrl(self, obj):
        if obj.coverPhotos:
            coverPhotos = json.loads(obj.coverPhotos)
            if len(coverPhotos) > 0:
                return coverPhotos[0]

        return ''

    class Meta:
        model = Item
        fields = "__all__"
